import { compare, isInArray } from "@/utils"

export default class CreepWorkExtension extends Creep {
    public HandMisson():void{
        // var a = Game.cpu.getUsed()
        if (!this.memory.role) return
        if (this.spawning) return
        if (!this.memory.Task) this.memory.Task = {}
        var taskList = Game.rooms[this.memory.belong].memory.Task['CREEP']
        var thisTaskList:RoomTask[] = []
        /* 过滤出属于自己的task */
        for (var Stask of taskList)
        {
            if (Stask.relateCreep && isInArray(Stask.relateCreep,this.memory.role))
                thisTaskList.push(Stask)
        }
        /* 没有该role相关任务就删除所有与任务有关的memory,并将所有资源送回storage */
        if (thisTaskList.length <= 0)
        {
            if (Game.time % 3 && Game.rooms[this.memory.belong].memory.zzZ)
                this.say("💤",)
            delete this.memory.inTask
            delete this.memory.bindTask
            delete this.memory.Task
            if (this.room.name != this.memory.belong) return
            var st = this.store
            if (Object.keys(st).length > 0)
            for (var i of Object.keys(st))
            {
                var storage_ = Game.getObjectById(Game.rooms[this.memory.belong].memory.storageID) as StructureStorage
                if (!storage_ ) return
                this.say("资源清理")
                this.transfer__(storage_,i as ResourceConstant)
                return
            }
            return
        }
        else
        {
            var task0:RoomTask
            if (thisTaskList.length > 1)
            {
                thisTaskList.sort(compare('taskLevel'))
            }
            /* 有bindData属性说明该任务允许多任务多工种 */
            if (thisTaskList[0].bindData)
            {
                if (this.memory.bindTask)
                {
                    LoopBind:
                    for (var t of thisTaskList)
                    {
                        if (this.memory.bindTask == t.ID)
                        {
                            task0 = t
                            break LoopBind
                        }
                    }
                    if (!task0)
                        delete this.memory.bindTask
                }
                else
                {
                    /* 还没有绑定的任务，就等待接取任务 */
                    LoopBind:
                    for (var t of thisTaskList)
                    {
                        if (t.bindData && t.bindData[this.memory.role] && t.bindData[this.memory.role].bind.length < t.bindData[this.memory.role].num)
                        {
                            t.bindData[this.memory.role].bind.push(this.name)
                            this.memory.bindTask = t.ID
                            break LoopBind
                        }
                    }
                    if (!this.memory.bindTask) {this.say("找不到任务了!");return}
                }
            }
            /* 单一任务爬虫 */
            else
            {
                task0 = thisTaskList[0]
            }
            /* 如果还没有任务，说明未找到任务，就等待 */
            if (task0) this.memory.inTask = task0.Name
            else return
            /* 任务超时计时 */
            task0.isBeHand = true
            switch (this.memory.inTask){
                case "链接送仓":{ this.handleLToS();break;}
                case "近塔填充":{ this.handleSToNT();break;}
                case "终端送仓":{ this.handleTToS();break;}
                case "仓送终端":{ this.handleSToT();break;}
                case "资源传送":{ this.handleTToT();break;}
                case "虫卵填充":{ this.handleFTE();break;}
                case "远塔填充":{ this.handleSToFT();break;}
                case "物流搬运":{ this.handleLT();break;}
                case "房间签名":{ this.HandleSG();break;}
                case "原矿开采":{ this.HandleRMH();break;}
                case "外矿开采":{ this.HandleOMH();break;}
                case "控制冲级":{ this.HandleCIL();break;}
                case "爬虫刷墙":{ this.HandleCRW();break;}
                case "强化刷墙":{ this.HandleBRW();break;}
                case "扩张援建":{ this.HandleEHB();break;}
                case "资源掠夺":{ this.HandleRP();break;}
                case "pb攻击":{this.HandlePB();break;}
                case "pb运输":{this.HandlePBC();break;}
                case "攻防一体":{this.HandleAIOA();break;}
                case "拆家一体":{this.HandleDSOA();break;}
                case "红球防御":{this.HandleDR();break;}
                case '黄球拆迁':{this.HandleDY();break;}
            }
        }
        // var b = Game.cpu.getUsed()
        // console.log("creep ",b-a)
    }
    
}
